package les.projeto;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class EditSemesterView extends Activity {
	public Calendar date;
	public int today;
	private String semesterName = "";
	private String initialDate = "";
	private String endDate = "";
	
	public void onCreate(Bundle savedInstanceState) {
		
		
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.edit_semester);
	    
	    initialDate = getIntent().getStringExtra("initialDate");
	    endDate = getIntent().getStringExtra("endDate");
	    semesterName = getIntent().getStringExtra("semesterName");
	
	    EditText name= (EditText)findViewById(R.id.editSemesterName);
	    name.setText(semesterName, TextView.BufferType.EDITABLE);

	    
	    date = Calendar.getInstance();
	    onNewIntent(getIntent());	    
	
	    
	    getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        
	    final DatePicker picker = (DatePicker)findViewById(R.id.datePicker2);
	    final DatePicker picker2 = (DatePicker)findViewById(R.id.DatePicker01);

	    picker.setCalendarViewShown(false);
	    picker2.setCalendarViewShown(false);
	    today = picker.getDayOfMonth();
	    
	    String[] date1 = initialDate.split("-");
	    String[] date2 = endDate.split("-");

	    picker.init(Integer.valueOf(date1[2]), Integer.valueOf(date1[1]), Integer.valueOf(date1[0]), null);
	    picker2.init(Integer.valueOf(date2[2]), Integer.valueOf(date2[1]), Integer.valueOf(date2[0]), null);

	    Button save = (Button)findViewById(R.id.buttonSave);
	    save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText name = (EditText)findViewById(R.id.editSemesterName);
				String nameText = name.getText().toString();
				
				
				if (nameText.isEmpty() || nameText.trim().equals("Enter Here")){
					AlertDialog.Builder builder = new AlertDialog.Builder(EditSemesterView.this);
					builder.setMessage("Please, enter a name")
					       .setCancelable(false)
					     
					       .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					builder.show();
				}else {
					
					int day = picker.getDayOfMonth();
					int month = picker.getMonth();
					int year = picker.getYear();
					
					String datePicker = day + "-" + month + "-" + year;
					String datePicker2 = picker2.getDayOfMonth() + "-" + picker2.getMonth() + "-" + picker2.getYear();
								
					editBD(semesterName, nameText, datePicker, datePicker2);
					
					AlertDialog.Builder builder = new AlertDialog.Builder(EditSemesterView.this);
					builder.setMessage("Semester save")
					       .setCancelable(false)
					     
					       .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					                back();
					           }
					       });
					builder.show();
									
				}
				
			}
		});
	    
	   

	    
	}
	
	protected void editBD(String name, String newName, String newInitialDate, String newEndDate) {
		BD bd = new BD();
		bd.editSemester(this, name, newName, newInitialDate, newEndDate);		
	}

	public void onNewIntent(Intent intent) {
		String dateText = intent.getStringExtra("date");
		String[] dateArr = dateText.split("-"); // date format is yyyy-mm-dd

		date.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
	}
	
	@Override
	public void onBackPressed() {
		
        back();	        		 
        return;
	}

	private void back() {
		int month = date.get(Calendar.MONTH);
        int year = date.get(Calendar.YEAR);
        
	    Intent trocatela = new Intent(EditSemesterView.this, SemestersView.class);
		trocatela.putExtra("date", year+"-"+month+"-"+today);

		EditSemesterView.this.startActivity(trocatela);
		EditSemesterView.this.finish();
	}

    private void removeSemester() {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);
        
        BD bd = new BD();
        bd.removeSemester(EditSemesterView.this, semesterName);
          
        Intent intent = new Intent(EditSemesterView.this,SemestersView.class);
        intent.putExtra("date", year+"-"+month+"-"+day);
        EditSemesterView.this.startActivity(intent);
        EditSemesterView.this.finish();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     super.onCreateOptionsMenu(menu);
     menu.add(0,0,0,"Delete semester");
     return true;
    } 
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
     super.onOptionsItemSelected(item);
     switch (item.getItemId()) {
        case 0:
            removeSemester();
            break;
     }
     return false;
    } 
}
